//
//  RBGroupVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/27.
//

#import "RBGroupVC.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "RBPager.h"
#import "RBImageBannerView.h"
#import "RBGroupListVC.h"
#import <UIView+YYAdd.h>
#import "RBGroupListResponse.h"
#import "UIViewController+RBAdd.h"
#import "RBMyGroupVC.h"

@interface RBGroupVC ()<UITextFieldDelegate>

@property(nonatomic, strong)UIView *bottomView;
@property(nonatomic, strong)RBPagerView *pager;
@property(nonatomic, strong)IBOutlet UIView *searchView;
@property(nonatomic, strong)IBOutlet UITextField *searchF;
@property(nonatomic, copy) NSString *searchText;
@property(nonatomic, strong)RBImageBannerView *bannerView;
@property(nonatomic, strong)NSMutableDictionary *vcsDicM;

@property(nonatomic, strong) RBGroupListResponse *response;

@end

@implementation RBGroupVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"拼团";
    [self.searchView zoomSubViewsWithSelf];
    self.searchView.height = Zoom(self.searchView.height);
}

- (void)fillData
{
    self.bannerView.dataSource = @[
        [UIImage imageNamed:@"b1.jpg"],
        [UIImage imageNamed:@"b2.jpg"],
        [UIImage imageNamed:@"b3.jpg"],
        [UIImage imageNamed:@"b4.jpg"],
    ];
    self.pager.hidden = NO;
}

- (RBGroupListVC *)vcWithIndex: (NSInteger)index
{
    RBGroupListVC *vc = self.vcsDicM[@(index)];
    if (!vc)
    {
        vc = RBGroupListVC.new;
        [self addChildViewController:vc];
        RBGroupListCatModel *model = self.response.group_cate[index];
        vc.id = model.cate_id;
        vc.searchText = self.searchText;
        self.vcsDicM[@(index)] = vc;
    }
    return vc;
}

#pragma mark - Network

- (void)getData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        
    } path:@"hu_group_list" post:YES log:YES responseClass:RBGroupListResponse.class desView:self.view showHud:YES showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:nil reloadBlock:^{
        [weak_self getData];
    }];
}

#pragma mark - Event

- (void)myGroupBtnTapped
{
    RBMyGroupVC *vc = RBMyGroupVC.new;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.view endEditing:YES];
    self.searchText = self.searchF.text;
    for (RBGroupListVC *vc in self.vcsDicM.allValues)
    {
        vc.searchText = self.searchText;
        [vc getData];
    }
    return YES;
}

#pragma mark - Setter

#pragma mark - Getter

- (UIView *)bottomView
{
    if (!_bottomView)
    {
        self.bottomView = ({
            UIView *view = UIView.new;
            [view rbSetBorderWitdh:1 borderColor:[RBThemeConfig lineGrayColor]];
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.bottom.mas_equalTo(-kRBSafeBottomHeight);
                make.height.mas_equalTo(Zoom(54));
            }];
            
//            [view h_view_addView:({
//                UILabel *view = [RanTool labelWithFontSize:Zoom(15) weight:1 color:[RBThemeConfig textBlackColor] align:1 text:@"拼团首页" bgColor:UIColor.whiteColor radius:0];
//                view;
//            }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth/2 top:0 bottom:0 left:0 right:CGFLOAT_MIN centerY:0];
            
            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(15) weight:1 color:UIColor.whiteColor align:1 text:@"我的拼团" bgColor:@"#E83922" radius:0];
                [view rbAddTapGesWithTarget:self action:@selector(myGroupBtnTapped)];
                view;
            }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:0 left:0 right:CGFLOAT_MIN centerY:0];
            
            view;
        });
    }
    return _bottomView;
}

- (RBPagerView *)pager
{
    if (!_pager)
    {
        self.pager = ({
            RBPagerView *pager = RBPagerView.new;
            __weak typeof(self) weakSelf = self;
            pager.getVcBlock = ^UIViewController * _Nonnull(NSInteger index) {
                return [weakSelf vcWithIndex:index];
            };
            CGFloat searchViewH = self.searchView.height;
            CGFloat bannerH = Zoom(145);
            CGFloat headerH = Zoom(40);
            CGFloat floatViewH = searchViewH + Zoom(15) + bannerH + Zoom(15) + headerH;
            [pager configWithHeaderStickTop:0 headerBottom:0 headerHeight:headerH floatViewHeight:floatViewH];
            [pager setupWithModel:({
                RBPagerHeaderModel *model = RBPagerHeaderModel.new;
                model.titles = ({
                    NSMutableArray *arrayM = NSMutableArray.array;
                    for (RBGroupListCatModel *model in self.response.group_cate)
                    {
                        [arrayM addObject:model.cate_name];
                    }
                    arrayM;
                });
                model.headerAttributeTitlesForNormalState = ({
                    NSMutableArray *arrayM = NSMutableArray.array;
                    for (NSString *title in model.titles)
                    {
                        [arrayM addObject:[RanTool attributeStringMaker:^(NSMutableArray *maker) {
                            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(14)] color:[RBThemeConfig textBlackColor] string:title];
                        }]];
                    }
                    arrayM;
                });
                model.headerAttributeTitlesForSelectedState = ({
                    NSMutableArray *arrayM = NSMutableArray.array;
                    for (NSString *title in model.titles)
                    {
                        [arrayM addObject:[RanTool attributeStringMaker:^(NSMutableArray *maker) {
                            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(16) weight:UIFontWeightMedium] color:[RBThemeConfig textBlackColor] string:title];
                        }]];
                    }
                    arrayM;
                });
                model.showSeperator = NO;
                model.buttonWidthStyle = RBPagerButtonWidthTitleWidthPlusPadding;
                model.headerButtonPadding = Zoom(10);
                model.bottomLineWidthStyle = RBPagerBottomLineWidthConstValue;
                model.bottomLineWidth = Zoom(22);
                model.bottomLineHeight = Zoom(3);
                model.bottomLineColor = [UIColor colorWithHexString:@"#5384FF"];
                model;
            })];
            [pager.pagerHeaderView.bottomLineView rbSetCornerRadius:Zoom(1.5)];
            pager.floatView.backgroundColor = UIColor.whiteColor;
            [pager.floatView addSubview:self.searchView];
            [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.height.mas_equalTo(self.searchView.height);
            }];
            [pager.floatView addSubview:self.bannerView];
            [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(Zoom(350));
                make.height.mas_equalTo(bannerH);
                make.centerX.mas_equalTo(0);
                make.top.mas_equalTo(self.searchView.mas_bottom).offset(Zoom(15));
            }];
            [self.view addSubview:pager];
            [pager mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.bottom.mas_equalTo(self.bottomView.mas_top);
            }];
            pager;
        });
    }
    return _pager;
}

- (RBImageBannerView *)bannerView
{
    if (!_bannerView)
    {
        self.bannerView = ({
            RBImageBannerView *view = RBImageBannerView.new;
            [view rbSetCornerRadius:Zoom(10)];
            __weak typeof(self) weakSelf = self;
            view.selectBlock = ^(NSInteger index) {
                
            };
            view;
        });
    }
    return _bannerView;
}

- (NSMutableDictionary *)vcsDicM
{
    if (!_vcsDicM)
    {
        self.vcsDicM = NSMutableDictionary.dictionary;
    }
    return _vcsDicM;
}

@end
